package com.bt.lms.ui.page.teaching.supervise;

import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import com.bt.lms.ui.page.sys.vo.TeacherListInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import java.util.ArrayList;
import java.util.List;

/**
 * 教师列表界面
 */
public class PageTeacherList extends VBox {

    /**
     * 查询条件
     */
    private final String text;

    /**
     * 构建教师列表界面
     *
     * @param text 查询条件
     */
    public PageTeacherList(String text) {

        //自己可能用的东西
        this.setId("PageTeacherList");
        this.text = text;

        //大小
        int wight = 1280;
        this.setPrefWidth(wight);
        this.setPrefHeight(650);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        //列表区域
        MyVBox listVBox = new MyVBox();
        listVBoxInit(listVBox);
        this.getChildren().add(listVBox);

        //搜索区域
        MyHBox searchHBox = new MyHBox();
        searchHBoxInit(searchHBox);
        this.getChildren().add(searchHBox);

    }

    /**
     * 搜索区域初始化
     *
     * @param searchHBox 搜索区域
     */
    private void searchHBoxInit(   HBox searchHBox) {

        //数据控制
        int width = 45;
        int sumStrip = 100;//总条数
        int present = 10;//当前页数
        String sumStripStr = "总记录数" + sumStrip + "条";
        String presentStr = "/" + present;

        //布局
        Insets insets = new Insets(10);
        searchHBox.setPadding(insets);
        searchHBox.setSpacing(10);
        searchHBox.setAlignment(Pos.CENTER_RIGHT);

        //组件
        Label firstLab = new Label("« 首页");
        searchHBox.getChildren().add(firstLab);
        Label lastLab = new Label("« 上一页");
        searchHBox.getChildren().add(lastLab);
        Button pageOne = new Button("1");
        searchHBox.getChildren().add(pageOne);
        Button pageTwo = new Button("2");
        searchHBox.getChildren().add(pageTwo);
        Button pageThree = new Button("3");
        searchHBox.getChildren().add(pageThree);
        Label nextLab = new Label("下一页 »");
        searchHBox.getChildren().add(nextLab);
        Label finallyLab = new Label("尾页 »");
        searchHBox.getChildren().add(finallyLab);
        Label sumNumberLab = new Label(sumStripStr);
        searchHBox.getChildren().add(sumNumberLab);
        TextField pageNumberTF = new TextField();
        pageNumberTF.setPrefWidth(width);
        searchHBox.getChildren().add(pageNumberTF);
        Label presentLab = new Label(presentStr);
        searchHBox.getChildren().add(presentLab);
        Button notarizeBtn = new Button("GO");
        searchHBox.getChildren().add(notarizeBtn);

    }

    /**
     * 列表区域初始化
     *
     * @param listVBox 列表区域
     */
    private void listVBoxInit(   MyVBox listVBox) {


        //列表
        //1.建立列表视图
        TableView<TeacherListInfo> tableView = new TableView<>();

        //2.列头
        //2.1 新建列头
        TableColumn<TeacherListInfo, HBox> operateTC = new TableColumn<>("操作");
        TableColumn<TeacherListInfo, String> nameTC = new TableColumn<>("教师姓名");
        TableColumn<TeacherListInfo, String> accountTC = new TableColumn<>("账号");
        TableColumn<TeacherListInfo, String> unitTC = new TableColumn<>("所属单位");
        TableColumn<TeacherListInfo, String> positionTC = new TableColumn<>("职位");
        TableColumn<TeacherListInfo, String> phoneTC = new TableColumn<>("移动电话");
        TableColumn<TeacherListInfo, String> relationTC = new TableColumn<>("联系电话");

        //2.2 列头添加到视图
        tableView.getColumns().addAll(operateTC, nameTC, accountTC, unitTC, positionTC, phoneTC, relationTC);

        //3.模拟数据
        //3.1 新建多个假数据
        List<TeacherListInfo> teacherCheckInInfos = simulatedData();
        //3.2 格式转化(用户数据集合转换成Javafx组件集合)
        ObservableList<TeacherListInfo> users = FXCollections.observableArrayList(teacherCheckInInfos);
        //3.3 数据添加到表格
        tableView.setItems(users);

        //4.映射（让每一列知道要展示的属性是哪一个）
        operateTC.setCellValueFactory(new PropertyValueFactory<>("operate"));
        nameTC.setCellValueFactory(new PropertyValueFactory<>("name"));
        accountTC.setCellValueFactory(new PropertyValueFactory<>("account"));
        unitTC.setCellValueFactory(new PropertyValueFactory<>("unit"));
        positionTC.setCellValueFactory(new PropertyValueFactory<>("position"));
        phoneTC.setCellValueFactory(new PropertyValueFactory<>("phone"));
        relationTC.setCellValueFactory(new PropertyValueFactory<>("relation"));

        //试图添加
        ObservableList<Node> tableChildren = listVBox.getChildren();
        tableChildren.add(tableView);

    }

    /**
     * 列表界面模拟数据
     *
     * @return 模拟好的数据集合
     */
    private    List<TeacherListInfo> simulatedData() {

        //数据总集合
        List<TeacherListInfo> teacherCheckInInfos = new ArrayList<>();

        //模拟第一条数据
        oneSimulateData(teacherCheckInInfos);

        //模拟第二条数据
        twoSimulateData(teacherCheckInInfos);

        //模拟第三条数据
        threeSimulateData(teacherCheckInInfos);

        //模拟第四条数据
        fourSimulateData(teacherCheckInInfos);


        //返回全部数据
        return teacherCheckInInfos;
    }

    /**
     * 第四条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void fourSimulateData(   List<TeacherListInfo> teacherCheckInInfos) {

        //第四条
        //操作盒子
        HBox operateHBox = operateHBoxInit();
        //界面数据对象
        TeacherListInfo teacherCheckInInfo = new TeacherListInfo(operateHBox, "李毅", "liyi@163.com", "西安大学",
                "教授", "13581801098", "010-89320912");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }


    /**
     * 第三条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void threeSimulateData(   List<TeacherListInfo> teacherCheckInInfos) {

        //第三条
        //操作盒子
        HBox operateHBox = operateHBoxInit();
        //界面数据对象
        TeacherListInfo teacherCheckInInfo = new TeacherListInfo(operateHBox, "李毅", "liyi@163.com", "西安大学",
                "教授", "13581801098", "010-89320912");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 第二条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void twoSimulateData(   List<TeacherListInfo> teacherCheckInInfos) {

        //第二条
        //操作盒子
        HBox operateHBox = operateHBoxInit();
        //界面数据对象
        TeacherListInfo teacherCheckInInfo = new TeacherListInfo(operateHBox, "李毅", "liyi@163.com", "西安大学",
                "教授", "13581801098", "010-89320912");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 第一条数据模拟
     *
     * @param teacherCheckInInfos 总数据集合
     */
    private void oneSimulateData(   List<TeacherListInfo> teacherCheckInInfos) {

        //第一条
        //操作按钮盒子
        HBox operateHBox = operateHBoxInit();
        //界面数据对象
        TeacherListInfo teacherCheckInInfo = new TeacherListInfo(operateHBox, "李毅", "liyi@163.com", "西安大学",
                "教授", "13581801098", "010-89320912");
        teacherCheckInInfos.add(teacherCheckInInfo);
    }

    /**
     * 操作按钮盒子初始化
     *
     * @return 操作按钮盒子
     */
    private    HBox operateHBoxInit() {

        //新建操作盒子
        HBox operateHBox = new HBox();
        operateHBox.setPadding(new Insets(2));
        operateHBox.setSpacing(10);

        //操作按钮
        RadioButton radioButton = new RadioButton();
        operateHBox.getChildren().add(radioButton);

        return operateHBox;
    }
}
